package com.zuxia.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zuxia.entity.Admin;
import com.zuxia.service.IAdminService;

@Controller("adminAction")
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/")
@Results({
	@Result(name="success",type="dispatcher",location="/admin/index.jsp"),
	@Result(name="login",type="dispatcher",location="/admin/login.jsp"),
	@Result(name="select",type="dispatcher",location="/admin/admin_Competence.jsp"),
	@Result(name="selectOne",type="dispatcher",location="/feedback.jsp"),
	@Result(name="selectNo",type="dispatcher",location="/contact.jsp"),
	@Result(name="selectId",type="dispatcher",location="/admin/admin_show.jsp"),
	@Result(name="selectNameId",type="dispatcher",location="/admin/admin_info.jsp"),
	@Result(name="yesDelect",type="dispatcher",location="/admin/admin_Competence.jsp"),
	@Result(name="updateAdmin",type="dispatcher",location="/admin/admin_info.jsp"),
	@Result(name="updateAdmin",type="dispatcher",location="/admin/admin_Competence.jsp"),
	@Result(name="error",type="dispatcher",location="/index.jsp"),
	@Result(name="json",type="json")
})
public class AdminAction extends ActionSupport implements ModelDriven<Admin>{
	private static final long serialVersionUID = 1L;
	
	private Admin admin = new Admin();
	private List<Admin> listAll;
	private List<Admin> listNameId;
	private String checks;//閻€劋绨崑姘闁插繐鍨归梽锟� 
	private boolean bol;

	HttpServletRequest request = ServletActionContext.getRequest();
	HttpSession session = request.getSession();
	
	@JSON(serialize=false)
	public List<Admin> getList() {
		return listAll;
	}
	public void setList(List<Admin> listAll) {
		this.listAll = listAll;
	}
	public Admin getModel() {
		return admin;
	}
	
	@JSON(serialize=false)
	public List<Admin> getListNameId() {
		return listNameId;
	}
	public void setListNameId(List<Admin> listNameId) {
		this.listNameId = listNameId;
	}

	public String getChecks() {
		return checks;
	}
	public void setChecks(String checks) {
		this.checks = checks;
	}
	public boolean isBol() {
		return bol;
	}
	public void setBol(boolean bol) {
		this.bol = bol;
	}

	@Resource(name="adminService")
	private IAdminService adminService;
	
	@Action("login")
	public String login(){
		listNameId = adminService.login(admin.getAdmin_name(),admin.getAdmin_pwd());
		if(listNameId.size()>0) {
			session.setAttribute("admin", listNameId.get(0));
			return "success";
		}
		return "login";
	}
	
	@Action("selectAdmin")
	public String selectAdmin(){
		try {
			listAll = adminService.selectAll();
			ActionContext.getContext().put("listAll", listAll);
			return "select";
		} catch (Exception e) {
			return "error";
		}
	}
	
	@Action("selectOne")
	public String selectOne(){
		try {
			listAll = adminService.selectAll();
			ActionContext.getContext().put("listAll", listAll);
			return "selectOne";
		} catch (Exception e) {
			return "error";
		}
	}
	
	@Action("selectNo")
	public String selectNo(){
		try {
			listAll = adminService.selectAll();
			ActionContext.getContext().put("listAll", listAll);
			return "selectNo";
		} catch (Exception e) {
			return "error";
		}
	}
	
	@Action("selectId")
	public String selectId() {
		try {
			this.admin = adminService.selectId(admin.getAdmin_id());
			return "selectId";
		} catch (Exception e) {
			return "error";
		}
	}
	
	@Action("selectNameId")
	public String selectNameId(){
		try {
			Admin admin = (Admin)session.getAttribute("admin");
			listNameId = adminService.login(admin.getAdmin_name(),admin.getAdmin_pwd());
			ActionContext.getContext().put("listNameId", listNameId);
			return "selectNameId";
		} catch (Exception e) {
			return "error";
		}
	}
	
	@Action("deleteAdmin")
	public String deleteMessage() {
		bol = adminService.deleteAdmin(admin);
		ActionContext.getContext().getValueStack().push(bol);
		return "json";
	}
	
	@Action("saveAdmin")
	public String saveAdmin() {
		bol = adminService.saveAdmin(admin);
		ActionContext.getContext().getValueStack().push(bol);
		return "json";
	}
	
	@Action("updateAdmin")
	public String updateAdmin() {
		return adminService.updateAdmin(admin);
	}
}
